import axios from 'axios';
import store from '@/store';
import lodash from 'lodash';
import router from '@/router';
import { message } from 'ant-design-vue';
// create an axios instance
const service = axios.create({
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 60000 // request timeout
});
const baseURL = '';

let debounceFlag = false;

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent

    // if (store.getters.token) {
    //   // let each request carry token
    //   // ['X-Token'] is a custom headers key
    //   // please modify it according to the actual situation
    //   config.headers['X-Token'] = getToken()
    // }
    return config;
  },
  error => {
    // do something with request error
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data;
    if (res.status === 200) {
      return res
    }
    if (res.status !== 0 && res.status !== -1) {
      // 302: 未登陆状态
      if (res.status === 302) {
        // if (res.data && process.env.NODE_ENV === 'production') {
        // 	window.open(res.data, '_self');
        // } else {
        // 	router.push('/login');
        // }
        const openUrl = `/task/ucs/login?referUrl=${encodeURIComponent(location.href)}`;
        window.open(openUrl, '_self');
      } else if (res.status === 201) {
        // Message({
        //   message: res.msg || '此用户不存在',
        //   type: 'error',
        //   duration: 3 * 1000
        // })
      } else {
        let callback = () => {
          setTimeout(() => {
            message.error(res.message);
            debounceFlag = false;
          }, 500);
        };
        if (!debounceFlag) {
          debounceFlag = true;
          callback();
        }
      }
      return Promise.reject(res);
      // return res
      // return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res;
    }
  },
  error => {
    console.log('err' + error); // for debug
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error);
  }
);

export default service;

export const doPost = (url, params) => {
  return service.post(baseURL + url, params);
};

export const doGet = (url, params) => {
  return service.get(baseURL + url, { params });
};

export const doPut = (url, params) => {
  return service.put(baseURL + url, params);
};

export const doDelete = (url, params) => {
  console.log(params);
  return service.delete(baseURL + url, params);
};


export const request = (api, options = {}) => {
  return new Promise((resolve, reject) => {
    fetch(api, options).then(
      response => {
        resolve(response.json())
      },
      err => {
        reject(err)
      }
    )
  })
}
